/*
 * *******************************************************************
 *   @项目名称: BHex Android
 *   @文件名称: FuturesHistoryOrderFragment.java
 *   @Date: 19-7-26 下午6:08
 *   @Author: ppzhao
 *   @Description:
 *   @Copyright（C）: 2019 BlueHelix Inc.   All rights reserved.
 *   注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的.
 *  *******************************************************************
 */

package io.bhex.app.account.ui;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.List;

import io.bhex.app.R;
import io.bhex.app.account.presenter.FuturesHistoryOrderFragmentPresenter;
import io.bhex.app.base.BaseListFreshFragment;
import io.bhex.app.trade.adapter.FuturesHistoryEntrustOrderAdapter;
import io.bhex.sdk.enums.ORDER_ENTRUST_TYPE;
import io.bhex.sdk.trade.futures.bean.FuturesOrderResponse;

public class FuturesHistoryOrderFragment extends BaseListFreshFragment<FuturesHistoryOrderFragmentPresenter, FuturesHistoryOrderFragmentPresenter.HistoryOrderFragmentUI> implements FuturesHistoryOrderFragmentPresenter.HistoryOrderFragmentUI{
    private SegmentTabLayout entrustTab;

    @Override
    protected FuturesHistoryOrderFragmentPresenter.HistoryOrderFragmentUI getUI() {
        return this;
    }

    @Override
    protected FuturesHistoryOrderFragmentPresenter createPresenter() {
        return new FuturesHistoryOrderFragmentPresenter();
    }

    @Override
    protected void initViews() {
        super.initViews();
        viewFinder.find(R.id.tabLinear).setVisibility(View.VISIBLE);
        entrustTab = viewFinder.find(R.id.tabEntrust);
        entrustTab.setTabData(new String[]{getString(R.string.string_ordinary_entrument),getString(R.string.string_planning_entrument),getString(R.string.string_stop_profit_stop_loss)});
    }

    @Override
    protected void addEvent() {
        super.addEvent();
        entrustTab.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                if (position ==0) {
                    //普通委托
                    getPresenter().getOrdinaryHistoryOrder(false);
                }else if(position ==1){
                    //计划委托
                    getPresenter().getPlanningHistoryOrder(false);
                }else if(position ==2){
                    //止盈止损
                    getPresenter().getStopProfitLossData(false);
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

    @Override
    public void showOrders(String entrustType, List<FuturesOrderResponse> currentOrders) {
        if (entrustType.equals(ORDER_ENTRUST_TYPE.LIMIT.getEntrustType())) {
            if (entrustTab.getCurrentTab()!=0) {
                //如果订单为普通委托，但是当前tab不在委托列表，不展示
                return;
            }
        }
        if (entrustType.equals(ORDER_ENTRUST_TYPE.STOP.getEntrustType())) {
            if (entrustTab.getCurrentTab()!=1) {
                //如果订单为计划委托，但是当前tab不在计划列表，不展示
                return;
            }
        }
        if (adapter == null) {
            adapter = new FuturesHistoryEntrustOrderAdapter(getActivity(), currentOrders);
            adapter.isFirstOnly(false);
            adapter.setOnLoadMoreListener(this,recyclerView);
            adapter.setEnableLoadMore(true);

            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            recyclerView.setItemAnimator(new DefaultItemAnimator());

            adapter.setEmptyView(emptyView);
            recyclerView.setAdapter(adapter);
        } else {
            adapter.setNewData(currentOrders);
        }
    }

}


